package com.example.zhinongbao.adapter;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.zhinongbao.R;
import com.example.zhinongbao.entity.Goods;
import com.example.zhinongbao.entity.Photo;
import com.example.zhinongbao.view.ProductDetailsActivity;

import java.util.List;

public class GoodsListAdapter extends RecyclerView.Adapter<GoodsListAdapter.ViewHolder> {

    private List<Goods> mList;

    public GoodsListAdapter(List<Goods> list){
        mList = list;
    }

    public void updateData(List<Goods> list) {
        mList.clear();
        mList = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goods_item, null);
        return new ViewHolder(view);
    }

    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        Goods goods = mList.get(position);
        holder.textView1.setText(goods.getName());
        holder.textView2.setText("¥ " + goods.getPrice() + "/" + goods.getUnit());
        List<Photo> photoList = goods.getPhotoList();
        if (photoList != null && photoList.size() > 0) {
            byte[] byteArray = photoList.get(0).getData();
            Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
            holder.photo.setImageBitmap(bitmap);
        } else {
            holder.photo.setImageResource(R.drawable.baseline_account_box_24);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null !=mOnItemClickListener ) {
                    mOnItemClickListener.onItemClick(goods,position);

                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView photo;
        TextView textView1;
        TextView textView2;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            photo = itemView.findViewById(R.id.shangpin_img);
            textView1 = itemView.findViewById(R.id.goods_name);
            textView2 = itemView.findViewById(R.id.goods_price);

        }
    }
    private  onItemClickListener mOnItemClickListener;



    public void setmOnItemClickListener(onItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public  interface  onItemClickListener{
        void  onItemClick(Goods goods,int position);
    }
    public void setData(List<Goods> goodsList) {
        this.mList.clear();
        this.mList.addAll(goodsList);
        notifyDataSetChanged();
    }
}
